package weaver.interfaces.hitech.duss.utils;

import com.weaver.general.BaseBean;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Node;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;


public class PostUtil {

	public static Map<String,String>  getXMLData(String pono,String sapcode) {//4100000021,QCAF0001
        Map<String,String> returnMap = new HashMap<>();
	    String json = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\">\n" +
                "   <soapenv:Header/>\n" +
                "   <soapenv:Body>\n" +
                "      <tem:CheckPOForBPM>\n" +
                "         <!--Optional:-->\n" +
                "         <tem:PONO>"+pono+"</tem:PONO>\n" +
                "         <!--Optional:-->\n" +
                "         <tem:SapCode>"+sapcode+"</tem:SapCode>\n" +
                "      </tem:CheckPOForBPM>\n" +
                "   </soapenv:Body>\n" +
                "</soapenv:Envelope>";
		try {
            StringBuffer result = PostUtil.toWebService(json,"http://172.27.108.41:8081/webservice/material.asmx?op=CheckPOForBPM" );
            Document document= DocumentHelper.parseText(result.toString());//xmlStr为上图格式的字符串
            Node RetFlag=document.selectSingleNode("//RetFlag");//获取节点对象,注意引号内的“//”必须加 ，否则报错
            Node MSG=document.selectSingleNode("//MSG");
            //根据节点对象获取相应信息
            String RetFlagValue=RetFlag.getText();
            String MSGValue=MSG.getText();
            returnMap.put("RetFlag",RetFlagValue);
            returnMap.put("MSG",MSGValue);
            System.out.println(returnMap.toString());
        }catch (Exception e){
		    e.printStackTrace();
        }
        return returnMap;
	}
	
    // 调用WS
    public static StringBuffer toWebService(String sendMsg,String INVOICE_WS_URL) throws Exception {
        //拼接请求报文

        // 开启HTTP连接ַ
        InputStreamReader isr = null;
        BufferedReader inReader = null;
        StringBuffer result = null;
        OutputStream outObject = null;
        try {
            URL url = new URL(INVOICE_WS_URL);
            HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();

            // 设置HTTP请求相关信息
            httpConn.setRequestProperty("Content-Length",
                    String.valueOf(sendMsg.getBytes().length));
            httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
            httpConn.setRequestMethod("POST");
            httpConn.setDoOutput(true);
            httpConn.setDoInput(true);

            // 进行HTTP请求
            outObject = httpConn.getOutputStream();
            outObject.write(sendMsg.getBytes());

            if (200 != (httpConn.getResponseCode())) {
                throw new Exception("HTTP Request is not success, Response code is " + httpConn.getResponseCode());
            }
            // 获取HTTP响应数据
            isr = new InputStreamReader(
                    httpConn.getInputStream(), "utf-8");
            inReader = new BufferedReader(isr);
            result = new StringBuffer();
            String inputLine;
            while ((inputLine = inReader.readLine()) != null) {
                result.append(inputLine);
            }
            return result;

        } catch (IOException e) {
            new BaseBean().writeLog("e::"+e);
            throw e;
        } finally {
            // 关闭输入流
            if (inReader != null) {
                inReader.close();
            }
            if (isr != null) {
                isr.close();
            }
            // 关闭输出流
            if (outObject != null) {
                outObject.close();
            }
        }

    }

}
